package com.dunke.web.html;

import com.dunke.core.common.TokenData;
import com.dunke.core.html.User;
import com.dunke.html.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * (DbAdmin)表控制层
 * 登录
 * @author makejava
 * @since 2021-06-23 18:37:25
 */
@Controller
@RequestMapping("/user")
public class LoginController {

    /**
     * 服务对象
     */
    @Resource
    private LoginService loginService;

    @RequestMapping("/login")
    public String test(Model model) {
        model.addAttribute("user",new User());
        return "login.html";
    }

    //post登录
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletRequest request, Model model,
                        @ModelAttribute(value="user") User user){
        System.out.println("输入的"+ user.getAccount() + ":" + user.getPassword());
        TokenData data = loginService.login(user);
        if (data == null) {
            model.addAttribute("msg", "用户名或密码错误");
            return "login";
        } else {
            //创建session对象
            HttpSession session = request.getSession();
            //把用户数据保存在session域对象中
            session.setAttribute("token", data.getToken());
            return "index.html";
        }
    }

    @RequestMapping("/list")
    public String user() {
        return "user/user_list.html";
    }

}
